package cn.ffcs.bpm.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.model.Transactor;

/**
 * 
 * 流程DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class TransactorDAO<D extends Transactor> extends BaseDAO<D> implements
        ITransactorDAO<D>
{

    /**
     * 获取共享资料实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new Transactor().getClass().getName();
    }

    public void remove(Object entity, HashMap<String, Object> conds)
    {
        String hql = "";
        if(conds!=null){
            hql = "delete from " + getEntityName() + " b where 1=1 ";
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                   hql+=" and "+key+"='"+ conds.get(key)+"'";
                }
            }
        }
        
        getSession().createQuery(hql).executeUpdate();
    }

    /**
     * 按查询条件获取总数
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<D> findBy(Criterion cond)
    {
        final Criterion conds = cond;
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {
            public List doInHibernate(Session session)
                    throws HibernateException
            {
                Criteria criteria = session.createCriteria(getEntityName());
                criteria.add(conds);
                return criteria.list();
            }
        });
    }

    /**
     * 按条件查询翻页
     * @param cond
     * @param currPage
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    public PageControl findByPage(Criterion cond, int currPage, int pageSize)
    {
        PageControl pageControl = new PageControl(findBy(cond).size(),
                currPage, pageSize);

        final int startRow = pageControl.getStart();
        final int endRow = pageControl.getPageSize();
        final Criterion conds = cond;

        List<D> results = getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(getEntityName());
                        criteria.add(conds);
                        // 设置翻页数据
                        criteria.setFirstResult(startRow);
                        criteria.setMaxResults(endRow);
                        return criteria.list();
                    }
                });
        return new PageControl(results, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
    }

    public List findByPage(HashMap<String, Object> conds)
    {
        String hql = "select businessRequest from Transactor transactor,TransactProcessStatus transactprocessstatus,BusinessRequest businessRequest where businessRequest.id=transactprocessstatus.refEntityId and transactprocessstatus.id=transactor.currProcess and businessRequest.end>sysdate ";
        if (conds != null)
        {
            //遍历参数表
            Iterator i = conds.entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (conds.get(key) != null
                        && !StringUtils.isEmpty(conds.get(key).toString()))
                {
                    if (key.equalsIgnoreCase("businessRequest.name")){
                        hql+=" and "+key+" like '%"+ conds.get(key)+"%'";
                    }else{
                        hql+=" and "+key+"='"+ conds.get(key)+"'";
                    }
                }
            }
        }
        hql+=" order by businessRequest.reqCode desc";
        return this.getSession().createQuery(hql).list();
    }

    /**
     * 
     * 取一定数据
     * @param num
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<D> findAll(int num,final String orderParam)
    {
        final int endRow=num;
        return getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(getEntityName());

                        // 设置翻页数据
                        criteria.setFirstResult(0);
                        criteria.setMaxResults(endRow);
                        
                        //按创建时间
                        criteria.addOrder(Order.desc(orderParam));
                        
                        return criteria.list();
                    }
                });
    } 
    
}
